package controller;

import java.awt.GridBagLayout;
import java.awt.event.*;
import java.util.Hashtable;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import model.Simulator;

/**
 * The class SpeedController extends AbstractController
 * @author Arjan Frans, Alwin Rombout, Jelte Verbree, Brendan Kanters
 * @version 1.0
 *
 */
public class SpeedController extends AbstractController implements ChangeListener {
	private static final long serialVersionUID = -3762097023651830639L;
    private JSlider slider;
    static final int FPS_MIN = 1;
    static final int FPS_MAX = 300;
    static final int FPS_INIT = 150;
        
    /**
     * Create a SpeedController using the simulator
     * @param simulator
     */
    public SpeedController(Simulator simulator)
    {
        super(simulator);
        
        slider = new JSlider(JSlider.HORIZONTAL, FPS_MIN, FPS_MAX, FPS_INIT);  
      //Create the label table
        slider.setPaintTicks(true);
        Hashtable labelTable = new Hashtable();
        labelTable.put( new Integer( FPS_MIN ), new JLabel("Fast") );
        labelTable.put( new Integer( FPS_MAX ), new JLabel("Slow") );
        slider.setLabelTable( labelTable );
        slider.setInverted(true);
        slider.setPaintLabels(true);
        slider.addChangeListener(this);
        this.add(slider);
        
   
    }

    @Override
    /**
     * Overrides the stateChanged method
     */
    public void stateChanged(ChangeEvent evt) {  
        JSlider slider = (JSlider)evt.getSource();  
  
        if (!slider.getValueIsAdjusting()) {  
            // Get new value  
            int value = slider.getValue();  
            simulator.setSpeed(value);
        }  
    }
}
